/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generator;

import net.db.generator.JavaConnectionClassWriter;

/**
 *
 * @author abosancic
 */
public class JavaSQLConnectionClassWriter extends JavaConnectionClassWriter {

    public JavaSQLConnectionClassWriter() {
        super();
    }

    //==========================================================================
    // setters and getters
    //==========================================================================
    protected void generateDAOClass() {

        String daoClass = spaces + "public static " + getClassName() + " getConnectionPool() {\n"
                + "        return connectionPool;\n"
                + "    }\n"
                + "\n"
                + "    public static void setConnectionPoolProperties(java.util.Properties properties) {\n"
                + "        try {\n"
                + "            connectionPool = new ConnectionPool(properties);\n"
                + "        } catch (Exception ex) {\n"
                + "            ex.printStackTrace();\n"
                + "        }\n"
                + "    }\n"
                + "    private static ConnectionPool connectionPool;\n"
                + "\n"
                + "    static {\n"
                + "        ResourceBundle bundle = PropertyResourceBundle.getBundle(\"net.etfbl.sqljdbc.SQLiteConnectionPool\");\n"
                + "        String driver = bundle.getString(\"driver\");\n"
                + "        String jdbcURL = bundle.getString(\"jdbcURL\");\n"
                + "        String username = bundle.getString(\"username\");\n"
                + "        String password = bundle.getString(\"password\");\n"
                + "        int preconnectCount = 0;\n"
                + "        int maxIdleConnections = 10;\n"
                + "        int maxConnections = 10;\n"
                + "        try {\n"
                + "            preconnectCount = Integer.parseInt(bundle.getString(\"preconnectCount\"));\n"
                + "            maxIdleConnections = Integer.parseInt(bundle.getString(\"maxIdleConnections\"));\n"
                + "            maxConnections = Integer.parseInt(bundle.getString(\"maxConnections\"));\n"
                + "        } catch (Exception ex) {\n"
                + "            ex.printStackTrace();\n"
                + "        }\n"
                + "        try {\n"
                + "            connectionPool = new ConnectionPool(driver,\n"
                + "                    jdbcURL, username, password,\n"
                + "                    preconnectCount, maxIdleConnections,\n"
                + "                    maxConnections);\n"
                + "        } catch (Exception ex) {\n"
                + "            ex.printStackTrace();\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    protected ConnectionPool(String aDriver,\n"
                + "            String aJdbcURL, String aUsername,\n"
                + "            String aPassword, int aPreconnectCount,\n"
                + "            int aMaxIdleConnections,\n"
                + "            int aMaxConnections)\n"
                + "            throws ClassNotFoundException, SQLException {\n"
                + "\n"
                + "        queue = new LinkedList();\n"
                + "        driver = aDriver;\n"
                + "        jdbcURL = aJdbcURL;\n"
                + "        username = aUsername;\n"
                + "        password = aPassword;\n"
                + "        preconnectCount = aPreconnectCount;\n"
                + "        maxIdleConnections = aMaxIdleConnections;\n"
                + "        maxConnections = aMaxConnections;\n"
                + "\n"
                + "        Class.forName(driver);\n"
                + "        for (int i = 0; i < preconnectCount; i++) {\n"
                + "            Connection conn = DriverManager.getConnection(\n"
                + "                    jdbcURL, username, password);\n"
                + "            conn.setAutoCommit(true);\n"
                + "            queue.add(conn);\n"
                + "        }\n"
                + "        connectCount = preconnectCount;\n"
                + "\n"
                + "    }\n"
                + "\n"
                + "    protected ConnectionPool(java.util.Properties properties)\n"
                + "            throws ClassNotFoundException, SQLException {\n"
                + "\n"
                + "        queue = new LinkedList();\n"
                + "        databaseProducName = properties.getProperty(\"databaseProducName\");\n"
                + "        driver = properties.getProperty(\"driver\");\n"
                + "        jdbcURL = properties.getProperty(\"jdbcURL\") + databaseProducName;\n"
                + "        username = properties.getProperty(\"user\");\n"
                + "        password = properties.getProperty(\"password\");\n"
                + "        preconnectCount = Integer.parseInt(properties.getProperty(\"preconnectCount\"));\n"
                + "        maxIdleConnections = Integer.parseInt(properties.getProperty(\"preconnectCount\"));\n"
                + "        maxConnections = Integer.parseInt(properties.getProperty(\"preconnectCount\"));\n"
                + "\n"
                + "        Class.forName(driver);\n"
                + "        for (int i = 0; i < preconnectCount; i++) {\n"
                + "            Connection conn = DriverManager.getConnection(\n"
                + "                    jdbcURL, username, password);\n"
                + "            conn.setAutoCommit(true);\n"
                + "            queue.add(conn);\n"
                + "        }\n"
                + "        connectCount = preconnectCount;\n"
                + "    }\n"
                + "\n"
                + "    public synchronized Connection checkOut()\n"
                + "            throws SQLException {\n"
                + "\n"
                + "        if (!queue.isEmpty()) {\n"
                + "            return queue.poll();\n"
                + "        }\n"
                + "        return null;\n"
                + "    }\n"
                + "\n"
                + "    public synchronized void checkIn(Connection aConn) {\n"
                + "        if (aConn == null) {\n"
                + "            return;\n"
                + "        }\n"
                + "        try {\n"
                + "            aConn.close();\n"
                + "            if (queue.size() < maxConnections) {\n"
                + "\n"
                + "                new Thread(new Runnable() {\n"
                + "                    @Override\n"
                + "                    public void run() {\n"
                + "                        Connection conn = null;\n"
                + "                        try {\n"
                + "                            conn = DriverManager.getConnection(\n"
                + "                                    jdbcURL, username, password);\n"
                + "                            conn.setAutoCommit(true);\n"
                + "                        } catch (SQLException ex) {\n"
                + "                            ex.printStackTrace();\n"
                + "                        }\n"
                + "                        queue.add(conn);\n"
                + "                    }\n"
                + "                }).start();\n"
                + "            }\n"
                + "        } catch (Exception ex) {\n"
                + "            ex.printStackTrace();\n"
                + "        }\n"
                + "\n"
                + "    }\n"
                + "\n"
                + "    public String getDatabaseProducName() {\n"
                + "        return databaseProducName;\n"
                + "    }\n"
                + "    private String driver;\n"
                + "    private String jdbcURL;\n"
                + "    private String username;\n"
                + "    private String password;\n"
                + "    private String databaseProducName;\n"
                + "    private int preconnectCount;\n"
                + "    private int connectCount;\n"
                + "    private int maxIdleConnections;\n"
                + "    private int maxConnections;\n"
                + "    private Queue<Connection> queue;\n"
                + "\n";
        stringClass.append(daoClass);
    }
}
